package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.projectSetup.forms.ProjectSetupForm;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.dao.UserDefinedFieldsDAO;
import com.scalar.forecast.projectSetup.ProjectSetup;
import com.scalar.forecast.forecastEntry.dao.ForecastEntrySearchDAO;
import com.scalar.forecast.salesPersonSetup.dao.SalesPersonSetupDAO;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Process the request to create new project setup
 *
 * Created by Veeru. 
 * Date: Feb 8, 2005
 * Time: 2:21:52 PM 
 */

public class ProjectSetupNewCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "projectSetup" );

        HashMap errors = new HashMap ();
        HttpSession session = request.getSession();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );

        ProjectSetupForm projectSetupForm = ( ProjectSetupForm )form;

        ProjectSetup  projectSetup = new ProjectSetup();
        projectSetupForm.setProjectSetup( projectSetup );
        int projectId = projectSetup.getProjectID();

        try {
            request.setAttribute( "customers", ForecastEntrySearchDAO.getAllCustomers( null ) );
            request.setAttribute( "odmsList", new ArrayList() );
            request.setAttribute( "endUserList", new ArrayList() );
            request.setAttribute( "salesPersons", SalesPersonSetupDAO.getSalesPersonList() );

            request.setAttribute( "availableChipsList" , ProjectSetupDAO.loadAvailableProducts( null, projectId, "CHIP") );
            request.setAttribute( "availableBoardsList" , ProjectSetupDAO.loadAvailableProducts( null, projectId, "BOARD") );
            request.setAttribute( "availableKitsList" , ProjectSetupDAO.loadAvailableProducts( null, projectId, "KIT") );

            request.setAttribute( "selectedChipsList" , new ArrayList() );
            request.setAttribute( "selectedBoardsList" ,  new ArrayList() );
            request.setAttribute( "selectedKitsList" ,  new ArrayList() );
            session.setAttribute( "customerCompulsory", ProjectSetupDAO.isCustomerSelected( null )+"" );

            projectSetup.setAvailableChipsList( ProjectSetupDAO.loadAvailableProducts( null, projectId, "CHIP") );
            projectSetup.setAvailableBoardsList( ProjectSetupDAO.loadAvailableProducts( null, projectId, "BOARD") );
            projectSetup.setAvailableKitsList( ProjectSetupDAO.loadAvailableProducts( null, projectId, "KIT"));
            projectSetup.setSelectedChipsList( new ArrayList() );
            projectSetup.setSelectedBoardsList( new ArrayList() );
            projectSetup.setSelectedKitsList( new ArrayList() );

            ArrayList userDefinedfields = UserDefinedFieldsDAO.loadUserDefinedFieldValues( null, projectId, "PROJSETUP" );
            projectSetup.setUserDefinedFeilds( userDefinedfields );

        } catch ( Exception e ){
            e.printStackTrace();
            actionForward = mapping.findForward( "projectSearch" );
        }

        return actionForward;
    }

}
